﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using Code.Models;
using System.Data.SqlClient;
using Code.DAL;

namespace Code.BLL
{
    public class UpRankManager : ManageBase<UpRank>
    {
        public UpRankInfo SetcteUpRank(Guid uid)
        {
            UpRank Up = base.Select<UpRank, UpRank>(s => s.FirstOrDefault(p => p.UsersId == uid));
            Rank ra = base.Select<Rank, Rank>(s => s.FirstOrDefault(p => p.Id == Up.RankId));
            var mode = base.Select<Rank, Rank>(s => s.FirstOrDefault(p => p.Exper > Up.ThisExpert));
            var nk = base.Select<Rank, Rank>(s => s.OrderByDescending(j => j.Ranks).FirstOrDefault(p => p.Exper > Up.ThisExpert));
            var exp = base.Select<Rank, Rank>(s => s.OrderByDescending(j => j.Ranks).FirstOrDefault());
            UpRankInfo info = new UpRankInfo();
            info.Rank = ra.Ranks;
            info.ThisEx = Up.ThisExpert;
            if (mode == null)
            {
                int b = Convert.ToInt32(exp.Exper);
                SetMode(b);
            }
            else
            {
                info.RankEx = mode.Exper;
            }

            return info;

        }
        public void SetMode(int ex)
        {
            string sql = "update dbo.UpRank set  ThisExpert=@e";
            using (SqlConnection conn = new SqlConnection(DBHelper.Connection.ConnectionString))
            {
                conn.Open();
                SqlCommand com = new SqlCommand(sql, conn);
                com.Parameters.AddWithValue("@e", ex);
                com.ExecuteNonQuery();
            }

        }
        public int DeleteUser(Guid uid)
        {
            int num = -1;
            string sql = "delete dbo.UpRank where   usersid=@e";
            using (SqlConnection conn = new SqlConnection(DBHelper.Connection.ConnectionString))
            {
                conn.Open();
                SqlCommand com = new SqlCommand(sql, conn);
                com.Parameters.AddWithValue("@e", uid);
                num = com.ExecuteNonQuery();
            }
            return num;
        }
        public int SetcteUpRank1(Guid uid)
        {
            UpRank Up = base.Select<UpRank, UpRank>(s => s.FirstOrDefault(p => p.UsersId == uid));
            Rank ra = base.Select<Rank, Rank>(s => s.FirstOrDefault(p => p.Id == Up.RankId));
            var mode = base.Select<Rank, Rank>(s => s.FirstOrDefault(p => p.Ranks == 1));
            int sa = mode.Exper;
            return sa;

        }
    }
    [Serializable]
    public class UpRankInfo
    {
        public int Rank { get; set; }
        public int ThisEx { get; set; }
        public int RankEx { get; set; }
    }
}




